export default class Arithmetic {
  min = 1
  max = 10
  isMinus = true
  workbookLen = 10
  operator = ['+', '-']

  constructor({ min = 1, max = 10, isMinus = true, workbookLen = 10 }) {
    this.min = min || this.min
    this.max = max || this.max
    this.isMinus = isMinus || this.isMinus  // 是否可以为负数
    this.workbookLen = workbookLen || this.workbookLen // 生成练习册题目数
  }
  rand(min, max) {
    min = min || this.min
    max = max || this.max
    return Math.round(Math.random() * (max - min) + min)
  }

  // 加法
  addition(fu1, fu2, symbolIndex) {
    const answer = fu1 + fu2
    return {
      fu1: fu1,
      fu2: fu2,
      answer
    }
  }
  // 减法
  subtraction(fu1, fu2, symbolIndex) {
    const isMinus = fu2 > fu1 && this.isMinus
    const answer = isMinus ? fu2 - fu1 : fu1 - fu2
    // console.log(isMinus, fu1 - fu2, '结果是否可以为负数')
    return {
      fu1: isMinus ? fu2 : fu1,
      fu2: isMinus ? fu1 : fu2,
      answer
    }
  }
  // 乘法
  multiplication(fu1, fu2, symbolIndex) {
    const answer = fu1 * fu2
    return {
      fu1: fu1,
      fu2: fu2,
      answer
    }
  }
  // 低阶除法
  lowDivision(fu1, fu2, symbolIndex) {
    fu1 = fu2 * this.rand(0, 10)
    const pd = fu1 / fu2
    return {
      fu1: fu1,
      fu2: fu2,
      answer: pd
    }
  }
  // 除法
  division(fu1, fu2, symbolIndex) {
    const isMinus = fu2 > fu1 && !this.isMinus
    const answer = isMinus ? fu2 / fu1 : fu1 / fu2
    // console.log(isMinus, fu1 - fu2, '结果是否可以为负数')
    return {
      fu1: isMinus ? fu2 : fu1,
      fu2: isMinus ? fu1 : fu2,
      answer: answer.toFixed(2)
    }
  }

  analogResult(answer) {
    let mockAnswer = this.rand(0, 1) === 1 ? answer - this.rand(1, 2) : answer + this.rand(1, 2)
    mockAnswer = answer >= 0 && mockAnswer < 0 ? -mockAnswer : mockAnswer
    mockAnswer = Math.round(Math.random(0, 1)) === 1 ? answer : mockAnswer
    // console.log(pd === arpd, 'result')
    return {
      mockAnswer,
      result: answer === mockAnswer
    }
  }

  // 生成练习题
  open() {
    const fu1 = this.rand() // 运算单位1
    const fu2 = this.rand() // 运算单位2
    const symbolIndex = this.rand(0, this.operator.length) // 运算符号symbolIndex => symbol：this.symbolList[symbolIndex]
    const symbol = this.operator[symbolIndex - 1]
    let result = {}
    // console.log(symbolIndex, symbol, 'symbol')
    // eslint-disable-next-line default-case
    switch (symbol) {
      case '+':
        result = this.addition(fu1, fu2, symbolIndex)
        break
      case '-':
        result = this.subtraction(fu1, fu2, symbolIndex)
        break
      case '*':
        result = this.multiplication(fu1, fu2, symbolIndex)
        break
      case '/':
        result = this.lowDivision(fu1, fu2, symbolIndex)
        break
    }
    const analogResult = this.analogResult(result.answer)
    return {
      ...result,
      ...{
        o: symbol,
        mockAnswer: analogResult.mockAnswer,
        result: analogResult.result,
        formula: result.fu1 + symbol + result.fu2 + '='
      }
    }
  }

  // 生成练习册
  workbook(workbookLen = 10) {
    const workbookList = []
    for (let index = 0; index < workbookLen; index++) {
      workbookList.push(this.open())
    }
    return workbookList
  }
}

// export default new Arithmetic({})
